package com.atos.optical.utils;

import com.atos.optical.model.bo.TreeNodeBo;
import com.atos.optical.model.po.SysMenuInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wanghra
 * @version 1.0
 * @description 树形节点工具类
 * @date 2025/8/13
 */
public class TreeUtil {
    public static List<TreeNodeBo> listToTree(List<SysMenuInfo> menuInfoList, String root) {
        List<TreeNodeBo> list = buildTreeNode(menuInfoList);
        List<TreeNodeBo> treeList = new ArrayList<>();
        for (TreeNodeBo nodeBo : list) {
            if (root.equals(nodeBo.getParentId())) {
                treeList.add(nodeBo);
            }
            for (TreeNodeBo t : list) {
                if (nodeBo.getValue().equals(t.getParentId())) {
                    if (null == nodeBo.getChildren()) {
                        nodeBo.setChildren(new ArrayList<>());
                    }
                    nodeBo.addChild(t);
                }
            }
        }
        return treeList;
    }

    public static List<TreeNodeBo> buildTreeNode(List<SysMenuInfo> list) {
        List<TreeNodeBo> treeList = new ArrayList<>();
        list.forEach(menuInfo -> treeList.add(TreeNodeBo.builder()
                .key(menuInfo.getMenuRouteName())
                .value(menuInfo.getMenuId())
                .title(menuInfo.getMenuName())
                .label(menuInfo.getMenuName())
                .url(menuInfo.getMenuUrl())
                .icon(menuInfo.getMenuIcon())
                .order(menuInfo.getMenuSort())
                .parentId(menuInfo.getParentId())
                .build()));
        return treeList;
    }
}
